package com.minimalist.basic.controller.lindi;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.vo.contracttemplate.LindiContractTemplateQueryVO;
import com.minimalist.common.entity.lindi.vo.contracttemplate.LindiContractTemplateVO;
import com.minimalist.common.feign.client.lindi.LindiContractTemplateFeignClient;
import com.minimalist.common.response.R;
import com.minimalist.common.utils.Add;
import com.minimalist.common.utils.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 林地指标平台_合同模板 Controller
 *
 * @author 11987
 */
@Validated
@RestController
@RequestMapping("/basic/lindi/contractTemplate")
@Tag(name = "林地指标平台_合同模板管理")
public class LindiContractTemplateController{
    private static final Logger log = LoggerFactory.getLogger(LindiContractTemplateController.class);


    @Autowired
    private LindiContractTemplateFeignClient feignClient;

    /**
     * 添加合同模板
     *
     * @param contractTemplateVO 合同模板信息
     * @return 成功或失败
     */
    @PostMapping("/addContractTemplate")
    @Operation(summary = "添加合同模板")
    @SaCheckPermission("lindi:contractTemplate:add")
    public R<?> addContractTemplate(
            @RequestBody @Validated(Add.class)
            LindiContractTemplateVO contractTemplateVO){
        contractTemplateVO.setCreateId(StpUtil.getLoginIdAsLong());
        contractTemplateVO.setUpdateId(StpUtil.getLoginIdAsLong());
        LocalDateTime now = LocalDateTime.now();
        contractTemplateVO.setCreateTime(now);
        contractTemplateVO.setUpdateTime(now);
        return feignClient.addContractTemplate(contractTemplateVO);
    }

    /**
     * 删除合同模板 -> 根据合同模板ID删除
     *
     * @param contractTemplateId 合同模板ID
     * @return 成功或失败
     */
    @DeleteMapping("/deleteContractTemplateByContractTemplateId")
    @Operation(summary = "删除合同模板 -> 根据合同模板ID删除")
    @SaCheckPermission("lindi:contractTemplate:delete")
    public R<?> deleteContractTemplateByContractTemplateId(
            @RequestParam("contractTemplateId")
            @NotNull(message = "合同模板ID不能为空")
            @Parameter(name = "contractTemplateId", required = true, description = "合同模板ID")
            Long contractTemplateId){
        return feignClient.deleteContractTemplateByContractTemplateId(contractTemplateId);
    }

    /**
     * 修改合同模板
     *
     * @param contractTemplateVO 合同模板信息
     * @return 成功或失败
     */
    @PutMapping("/updateContractTemplateByContractTemplateId")
    @Operation(summary = "修改合同模板")
    @SaCheckPermission("lindi:contractTemplate:update")
    public R<?> updateContractTemplateByContractTemplateId(
            @RequestBody @Validated(Update.class)
            LindiContractTemplateVO contractTemplateVO){
        contractTemplateVO.setUpdateId(StpUtil.getLoginIdAsLong());
        contractTemplateVO.setUpdateTime(LocalDateTime.now());
        return feignClient.updateContractTemplateByContractTemplateId(contractTemplateVO);
    }

    /**
     * 查询合同模板列表(分页)
     *
     * @param queryVO 查询条件
     * @return 合同模板列表
     */
    @GetMapping("/getPageContractTemplateList")
    @Operation(summary = "查询合同模板列表(分页)")
    @SaCheckPermission("lindi:contractTemplate:get")
    public R<PageResp<LindiContractTemplateVO>> getPageContractTemplateList(
            LindiContractTemplateQueryVO queryVO){
        return feignClient.getPageContractTemplateList(queryVO);
    }

    /**
     * 根据合同模板ID查询合同模板
     *
     * @param contractTemplateId 合同模板ID
     * @return 合同模板
     */
    @GetMapping("/getContractTemplateByContractTemplateId/{contractTemplateId}")
    @Operation(summary = "根据合同模板ID查询合同模板")
    @SaCheckPermission("lindi:contractTemplate:get")
    public R<LindiContractTemplateVO> getContractTemplateByContractTemplateId(
            @PathVariable(value = "contractTemplateId")
            @NotNull(message = "合同模板ID不能为空")
            @Parameter(name = "contractTemplateId", description = "合同模板ID", required = true)
            Long contractTemplateId){
        return feignClient.getContractTemplateByContractTemplateId(contractTemplateId);
    }

    @PostMapping("/deleteContractTemplateFileByFileId")
    @Operation(summary = "删除合同模板文件 -> 根据合同模板文件ID删除")
    @SaCheckPermission("lindi:contractTemplate:delete")
    public R<?> deleteContractTemplateFileByFileId(
            @RequestParam("fileId") @NotNull(message = "合同模板文件ID不能为空")
            @Parameter(name = "fileId", required = true, description = "合同模板文件ID")
            Long fileId){
        return feignClient.deleteContractTemplateFileByFileId(fileId);
    }
}
